package com.swingson.properties.impl;

import com.swingson.SwingsonGui;
import com.swingson.exceptions.SwingsonException;
import com.swingson.jsonparser.JsonElement;
import com.swingson.layout.swingsonlayout.SwingsonLayout;
import com.swingson.properties.BasePropertyProcessor;
import java.awt.Container;
import javax.swing.JComponent;

/**
 * @author sad
 */
public class PropertyProcessors {
public abstract static class PrimitivePropertyProcessor extends BasePropertyProcessor {

        @Override
        public Object processProperty(JsonElement jsonElement, JComponent component, SwingsonGui gui, String propertyName) {
            if (!jsonElement.isPrimitive()) {
                throw new SwingsonException("Property " + propertyName + " in class " + component.getClass().getSimpleName() + " should be primitive value");
            }

            return getValue(jsonElement, component, gui, propertyName);
        }

        protected abstract Object getValue(JsonElement jsonElement, JComponent component, SwingsonGui gui, String propertyName);
    }

    public static class StringPropertyProcessor extends PrimitivePropertyProcessor {

        @Override
        protected Object getValue(JsonElement jsonElement, JComponent component, SwingsonGui gui, String propertyName) {
            return jsonElement.getAsString();
        }
    }

    public static class BooleanPropertyProcessor extends PrimitivePropertyProcessor {

        @Override
        protected Object getValue(JsonElement jsonElement, JComponent component, SwingsonGui gui, String propertyName) {
            return jsonElement.getAsBoolean();
        }
    }

    public static class FloatPropertyProcessor extends PrimitivePropertyProcessor {

        @Override
        protected Object getValue(JsonElement jsonElement, JComponent component, SwingsonGui gui, String propertyName) {
            return jsonElement.getAsFloat();
        }
    }

    public static class IntegerPropertyProcessor extends PrimitivePropertyProcessor {

        @Override
        protected Object getValue(JsonElement jsonElement, JComponent component, SwingsonGui gui, String propertyName) {
            return jsonElement.getAsInt();
        }
    }

    public static class ShortPropertyProcessor extends PrimitivePropertyProcessor {

        @Override
        protected Object getValue(JsonElement jsonElement, JComponent component, SwingsonGui gui, String propertyName) {
            return (short)jsonElement.getAsInt();
        }
    }

    public static class LongPropertyProcessor extends PrimitivePropertyProcessor {

        @Override
        protected Object getValue(JsonElement jsonElement, JComponent component, SwingsonGui gui, String propertyName) {
            return jsonElement.getAsLong();
        }
    }

    public static class DoublePropertyProcessor extends PrimitivePropertyProcessor {

        @Override
        protected Object getValue(JsonElement jsonElement, JComponent component, SwingsonGui gui, String propertyName) {
            return (double)jsonElement.getAsFloat();
        }
    }

    public static class CharPropertyProcessor extends PrimitivePropertyProcessor {

        @Override
        protected Object getValue(JsonElement jsonElement, JComponent component, SwingsonGui gui, String propertyName) {
            return (char)jsonElement.getAsString().charAt(0);
        }
    }
}
